package com.xujiang.data.model;

import android.content.Context;
import android.support.annotation.NonNull;
import android.text.TextUtils;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.xujiang.common_core.util.tool.ResourceUtils;
import com.xujiang.common_core.util.mlog.L;
import com.xujiang.common_core.util.ourexception.ISystemException;
import com.xujiang.common_core.util.ourexception.NetLocalException;
import com.xujiang.common_core.util.ourexception.NetServerException;
import com.xujiang.common_core.util.ourexception.SystemException;
import com.xujiang.data.R;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;

import java.net.ConnectException;
import java.net.NoRouteToHostException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;

/**
 * @author xujiang
 * @version 2017-07-20 11:47
 */
@JsonIgnoreProperties(ignoreUnknown = true)
public class ServerError {

    public static String getServerError(Context context, SystemException e) {
        String message = e.getMessage();
        if (!TextUtils.isEmpty(message)) {
            if (message.equals(ISystemException.DATA_ERROR)) {
                return context.getString(R.string.err_data_exceptional);
            }
        }
        return getServerError(context, e.getCause());
    }

    public static String getServerError(Context context, Throwable e) {
        if (e instanceof NetServerException) {
            NetServerException exception = (NetServerException) e;
            Object errorBody = exception.getErrorBody();
            if (errorBody instanceof String) {
                return exception.getErrorBody();
            } else if (errorBody instanceof ServerError) {
                ServerError error = (ServerError) errorBody;
                String errorString = getErrorString(context, error.getCode());
                //TOKEN过期时强制登录
                if (4001004 == error.getCode()) {
                    EventBus.getDefault().post(new LogInEvent(errorString));
                    return "";
                }
                return errorString;
            }
        } else {
            if (e instanceof SocketTimeoutException) {
                return context.getString(R.string.ERROR_NETWORK_TIMEOUT);
            } else if (e instanceof ConnectException) {
                return context.getString(R.string.ERR_DEVICE_CAN_NOT_CONNECT_TO_SERVER);
            } else if (e instanceof UnknownHostException) {
                return context.getString(R.string.ERR_NETWORK);
            } else if (e instanceof NoRouteToHostException) {
                return context.getString(R.string.ERR_NETWORK);
            }else if (e instanceof NetLocalException){
                if (e.getCause() instanceof JSONException){
                    return context.getString(R.string.ERR_SERVER);
                }else if (!TextUtils.isEmpty(e.getMessage())){
                    return e.getMessage();
                }
                return context.getString(R.string.ERR_INTERNAL);
            }
        }
        return context.getString(R.string.ERR_UNKNOWN);
    }

    @NonNull
    public static String getErrorString(Context context, long code) {
        String re = ResourceUtils.getStringResourceByName(context, "_" + code);  //获取错误码对应的字符串 在 error_string.xml
        if (TextUtils.isEmpty(re)) {
            return context.getString(R.string.ERR_UNKNOWN) + " " + code;
        } else {
            return re;
        }
    }

    public static void error(String tag, Context context, SystemException e) {
        String serverError = getServerError(context, e);
        if (!TextUtils.isEmpty(serverError)) {
            L.getToast().e(tag, context, serverError, e);
        }
    }

    public static void error(String tag, Context context, Throwable e) {
        String serverError = getServerError(context, e);
        if (!TextUtils.isEmpty(serverError)) {
            L.getToast().e(tag, context, serverError, e);
        }
    }

    private long code;
    private String msg;

    public ServerError() {
    }

    public ServerError(long code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public long getCode() {
        return code;
    }

    public void setCode(long code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    @Override
    public String toString() {
        return "ServerError{" +
                "code='" + code + '\'' +
                ", msg='" + msg + '\'' +
                '}';
    }

}
